package com.dhc.appbusinessfrm.presenter;

import android.text.TextUtils;

import com.dhc.appbusinessfrm.business.common.NetworkBusiness;
import com.dhc.appbusinessfrm.business.login.LoginBusiness;
import com.dhc.appbusinessfrm.business.login.LoginCallback;
import com.dhc.appbusinessfrm.model.ResultModel;
import com.dhc.appbusinessfrm.util.ActivityUtils;
import com.dhc.appbusinessfrm.util.MD5;
import com.dhc.appbusinessfrm.view.activity.WelcomeInteractor;

import java.util.concurrent.Future;

import javax.inject.Inject;

/**
 * Created by xy on 2015/6/11.
 */
public class WelcomePresenterImpl extends BasePresenterImpl implements WelcomePresenter {

    private WelcomeInteractor welcomeInteractor;

    public WelcomePresenterImpl(WelcomeInteractor welcomeInteractor) {
        super(welcomeInteractor);
        this.welcomeInteractor = welcomeInteractor;
    }

    /**
     * 注入网络业务类
     */
    @Inject
    NetworkBusiness networkBusiness;

    /**
     * 注入登录业务类
     */
    @Inject
    LoginBusiness loginBusiness;

    @Override
    public void ini() {
        // 网络是否连接
        if (!networkBusiness.isNetworkConnected(welcomeInteractor.getMyContext())) {
            welcomeInteractor.createNetworkNotAvailableDialog();
            //TODO 返回
            return;
        }
        // 自动登录
        autoLogin();
    }


    /**
     * 自动登录
     */
    public void autoLogin() {
        String loginName = preferenceUtils.getLoginName();
        String password = preferenceUtils.getPassword();

        if (TextUtils.isEmpty(loginName) || TextUtils.isEmpty(password)) {
            // 跳转至登录界面
            ActivityUtils.startLoginActivity(welcomeInteractor.getMyContext());
            // 关闭本窗体
            welcomeInteractor.finishActivity();
        } else {
            // 登录
            Future future = loginBusiness.login(loginName, MD5.getMD5Str(password), loginCallback);
        }
    }

    /**
     * 登录业务回调函数
     */
    LoginCallback loginCallback = new LoginCallback() {

        @Override
        public void netSuccess(ResultModel resultModel) {
            //TODO 跳转至首页

            // 关闭本窗体
            welcomeInteractor.finishActivity();
        }

        @Override
        public void netFailure(int errorId) {
            //TODO 跳转至登录界面

            // 关闭本窗体
            welcomeInteractor.finishActivity();

        }
    };


}
